package com.cmcc.migu.cmmon.util;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/** 
 *
 *
 * @Project: UIM-COMMON
 * @File: UIMAESUtils.java 
 * @Date: 2015年5月7日 
 * @Author: 李宏利
 * @Copyright: 版权所有 (C) 2015 中国移动 杭州研发中心. 
 *
 * @注意：本内容仅限于中国移动内部传阅，禁止外泄以及用于其他的商业目的 
 */

public class UIMAESUtil {
	private static Logger logger = LoggerFactory.getLogger(UIMAESUtil.class);
	
	/**
	 * 解密请求的加密字符串，返回其中的信息
	 * @param requesterKey	当前请求用户的密钥
	 * @param encryptParams	加密字符串
	 * @return
	 * 			requesterCode 请求者编号
	 * 			serviceCode 应用Code
	 * 			paramMapJson 请求参数Map转成json
	 * @throws Exception 
	 */
	public static Map<String, String> decryptor(String requesterKey, String encryptParams) throws Exception{
		Map<String, String> result = new HashMap<String, String>();
		AESUtil aesUtil = null;
		try {
			aesUtil = AESUtil.getInstance(requesterKey);
		} catch (Exception e) {
			logger.error("the method decryptor in class UIMAESUtils throw Exception, Instance AESUtil err", e);
			throw new Exception("实例化AESUtil失败");
		}
		if(StringUtils.isBlank(encryptParams)){
			logger.error("the method decryptor in class UIMAESUtils throw Exception, the encryptParams is " + encryptParams);
			throw new Exception("请求参数为空");
		}
		String[] params = encryptParams.split("@");
		//2.8位随机字符串@requesterCode@serviceCode用requesterKey加密@paramMap转化成json字符串用requesterKey加密
		if(params.length > 0){
			result.put("authCode", params[1]);
			String otherMapStr = aesUtil.decryptor(params[2]);
			String businessMapStr = aesUtil.decryptor(params[3]);
			result.put("otherMapStr", otherMapStr);
			result.put("businessMapStr", businessMapStr);
		}
		return result;
	}
	
	/**
	 * 对请求信息做加密
	 * @param requesterCode		访问者Code
	 * @param serviceCode		请求的服务Code
	 * @param paramMap			请求的参数map
	 * @param requesterKey		请求者密钥
	 * @return
	 * @throws Exception 
	 */
	public static String encrypt(String requesterCode, Map<String, Object> otherMap, Map<String, Object> businessMap, String requesterKey) throws Exception{
		AESUtil aesUtil = null;
		try {
			aesUtil = AESUtil.getInstance(requesterKey);
		} catch (Exception e) {
			logger.error("the method encrytor in class UIMAESUtils throw Exception, Instance AESUtil err", e);
			throw new Exception("实例化AESUtil失败");
		}
		
		//8位随机字符串@requesterCode@其他参数Map转成json字符串用requesterKey加密@businessMap转化成json字符串用requesterKey加密
		String otherMapSecret = aesUtil.encrytor(JSONObject.fromObject(otherMap).toString());
		String businessMapSecret = aesUtil.encrytor(JSONObject.fromObject(businessMap).toString());
		StringBuffer result = new StringBuffer();
		result.append(getRandomStr(8));
		result.append("@");
		result.append(requesterCode);
		result.append("@");
		result.append(otherMapSecret);
		result.append("@");
		result.append(businessMapSecret);
		return result.toString();
	}
	
	/**
	 * 生成固定长度的随机数
	 * @param size	随机数长度
	 * @return
	 */
	private static String getRandomStr(Integer size){
		Random random = new Random();
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < size; i++) {
			Integer singleNum = random.nextInt(10);
			result.append(singleNum.toString());
		}
		return result.toString();
	}

}
